﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Windows.Forms;
using PayexpDAL;
using Utilities;
using System.Globalization;
using ExcelHandler;

namespace Moneymatters
{
    public partial class MainForm
    {
        #region Private Fields
        private bool _loadPettyCashView = true;
        #endregion Private Fields

        #region Properties
        #endregion Properties

        #region Private Methods

        private void dataGridViewPettyCash_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewPettyCash.SelectedCells.Count == 1)
            {
                try
                {
                    int pettyCashId = (int)this.dataGridViewPettyCash.SelectedCells[0].Tag;
                    List<int> tranasactionMasterIdList = this.dataAccessHandlerCommonInstance.GetTransactionMasterIdListByPettyCashIdList(new List<int>() { pettyCashId },
                                                                                        MainForm.Context);

                    if (tranasactionMasterIdList.Count == 0)
                    {
                        MessageBox.Show("There are no transactions to display. No cash expense has been made against this petty cash withdrawal entry.", "No Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //this.DisplaySpecificTransactions(tranasactionMasterIdList);
                        DisplayPettyCashTransactions(tranasactionMasterIdList, pettyCashId);
                        this.tabControl.SelectedIndex = 1;
                    }
                    //this.Close();
                }
                catch
                {
                    // easy way of handling cell click on other cells :)
                }
            }
        }

        private void buttonExportPettyCashTransactions_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Will be implemented sooner. Should be able to export details of the petty cash withdrawal and its related cash expense transactions by rolling over transactions where needed.", "Coming soon!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void DisplayPettyCashGridView()
        {
            if (!_loadPettyCashView)
            {
                return;
            }

            this.dataGridViewPettyCash.Rows.Clear();
            int totalEntries = this.dataAccessHandlerCommonInstance.GetPettyCashEntrycount(MainForm.Context);
            int entriesFetched = 0;
            int currentPageNumberForViewPettyCash = 0;
            int numberOfRecordsPerPage = this.NumberOfRecordsPerPage;
            while (entriesFetched < totalEntries)
            {
                List<PettyCash> pagedResult = this.dataAccessHandlerCommonInstance.GetPettyCashList(currentPageNumberForViewPettyCash, numberOfRecordsPerPage, MainForm.Context);
                entriesFetched = entriesFetched + pagedResult.Count();
                foreach (PettyCash pettyCash in pagedResult)
                {
                    this.dataGridViewPettyCash.Rows.Add();
                    int newRowIndex = this.dataGridViewPettyCash.Rows.Count - 1;
                    DataGridViewRow newRow = this.dataGridViewPettyCash.Rows[newRowIndex];

                    newRow.Cells["pettyCashId"].Value = pettyCash.Id;
                    newRow.Cells["pettyCashAmount"].Value = pettyCash.Amount;
                    newRow.Cells["addedBy"].Value = pettyCash.CreatedBy;
                    newRow.Cells["addedOn"].Value = pettyCash.CreatedDate.Get_DD_MONTH_YY_DateString(); ;
                    newRow.Cells["withdrawalDate"].Value = pettyCash.DateWithdrawnFromBank.Value.Get_DD_MONTH_YY_DateString();
                    newRow.Cells["balance"].Value = pettyCash.BalanceAmount;
                    if (pettyCash.UsedUpDate.HasValue)
                    {
                        newRow.Cells["usedUpDate"].Value = pettyCash.UsedUpDate.Value.Get_DD_MONTH_YY_DateString();
                    }
                    else
                    {
                        newRow.Cells["usedUpDate"].Value = "Not yet used up";
                    }

                    newRow.Cells["narrationPettyCash"].Value = pettyCash.Narration;
                    newRow.Cells["viewTransactions"].Value = "View Transaction(s)";
                    newRow.Cells["viewTransactions"].Tag = pettyCash.Id;

                    newRow.Cells["totalNoOfPettyCashTransactions"].Value = pettyCash.PettyCashUsages.Where(t => t.IsActive).Count();
                    pettyCash.SetRollOverCountAndAmount(MainForm.Context);
                    newRow.Cells["noOfRolleOverTransactions"].Value = pettyCash.RollOverCount;
                    newRow.Cells["rollOverAmount"].Value = pettyCash.RollOverAmount;

                    newRow.Tag = pettyCash;
                }
                currentPageNumberForViewPettyCash++;
            }

            if (this.dataGridViewPettyCash.Rows.Count == 0)
            {
                MessageBox.Show("No petty cash entries to display", "No entries", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                this._loadPettyCashView = false;
            }
        }

        #endregion Private Methods

        #region Public Methods
        #endregion Public Methods
    }
}